<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
           <a-col :span="24" v-if="flag">
            <a-form-item label="单位名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-depart v-decorator="['orgName',validatorRules.orgName]"  @change="change" />
            </a-form-item>
          </a-col>
          <a-col :span="24" v-if="flag2">
            <a-form-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
               <j-search-select-tag  placeholder="请选择项目名称" v-decorator="['productBelong', validatorRules.belongText]" :trigger-change="true" dictCode="" type="list" setFieldsValue="selectValue" :dictOptions="PValue" @change="changePValue"> </j-search-select-tag>
            </a-form-item>
          </a-col>
          <a-col :span="24" v-if="flag">
            <a-form-item label="代码类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
                <j-search-select-tag id="name" placeholder="请输入代码类型" v-decorator="['codeSortId', validatorRules.textName]" :trigger-change="true" dictCode="" type="list" setFieldsValue="selectValue" :dictOptions="textName" v-on:show="handleChange" @change="changeSort"> </j-search-select-tag>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="代码名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['dtPValue', validatorRules.dtPValue]" placeholder="请输入代码名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['content3']" placeholder="请输入备注"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24" v-if="flag1">
            <a-form-item label="所属父类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input value="物料大类" disabled></a-input>
                <j-search-select-tag  placeholder="请输入所属父类" v-decorator="['content5', validatorRules.text]" :trigger-change="true" dictCode="" type="list" setFieldsValue="selectValue" :dictOptions="PCode"> </j-search-select-tag>
            </a-form-item>
          </a-col>
          
         
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'  
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import JMultiSelectTag from "@/components/dict/JMultiSelectTag"
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import {getCookie} from '@/utils/cookie.js'
  import store from '@/store'

  export default {
    textName: 'DtPCodeForm',
    components: {
      JFormContainer,
      JDate,
      JSelectDepart,
      JMultiSelectTag,
      JSearchSelectTag,
      JDictSelectTag,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        name:'',
        productBelong:'',
        departIds:'',
        flag:true,
        flag1:false,
        flag2:true,
        selectValue:"",
        asyncSelectValue:"",
        textName:[],
        PValue:[],
        PCode:[],
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          orgName: {
            rules: [
              { required: true, message: '请输入所属风场!'},
            ]
          },
          textName: {
            rules: [
              { required: true, message: '请输入代码类型!'},
            ]
          },
          dtPCode: {
            rules: [
              { required: true, message: '请输入所属父类!'},
            ]
          },
          dtPValue: {
            rules: [
              { required: true, message: '请输入代码名称!'},
            ]
          },
          createBy: {
            rules: [
              { required: true, message: '请输入创建人!'},
            ]
          },
          belongText:{
            rules: [
              { required: true, message: '请输入项目名称!'},
            ]
          }
        },
        url: {
          add: "product_manage/dtPCode/add",
          edit: "/product_manage/dtPCode/edit",
          queryById: "/dtpcode/dtPCode/queryById",
          dtPCode:'product_manage/dtPCode/queryCodeType',
          PValue:'product_manage/dtPCode/querycodesubtypeByParam'

        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add (record) {
         var obj={}
      obj.orgName = store.getters.userInfo.departIds
      console.log(obj)
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(obj,'orgName'))  //使添加的时候所属风场是登录人所在的风场
      })
        this.edit({});
      },
      edit (record) {
        if(record.id != null){
          this.flag = false
          this.flag2 = false
        }else{
          this.flag = true
          this.flag2 = true
        }
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'textName','codeType','dtPCode','dtPValue','orgName','createTime','createBy','content3','belongText','text'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log('values',values)
            console.log("表单提交数据",formData)
            formData.createBy = getCookie('username')
            formData.content1 = store.getters.userInfo.id
            formData.name = this.name
            formData.codeType = ''
            formData.content4 = ''
            // formData.
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'textName','codeType','dtPCode','dtPValue','orgName','createTime','createBy','content3','belongText','text'))
      },
      handleChange(value) { 
        console.log("selected", value); 
        if(value == '200000'){
          console.log('物料小类')
          this.name = '物料小类'
          this.flag1 = true
          this.flag2 = true
        }else if(value == '600000'){
          this.name = '所属项目'
          this.flag1 = false
           this.flag2 = false
        }else{
          this.flag1 = false
          this.flag2 = true
        }

        if(value == '100000'){
          this.name = '物料大类'
        }else if(value == '300000'){
          this.name = '生产厂家'
        }else if(value == '400000'){
          this.name = '规格型号'
        }else if(value == '500000'){
          this.name = '物料名称'
        }else if(value == '800000'){
          this.name = '单位'
        }else if(value == '900000'){
          this.name = '工具类别'
        }
        },
      change(value){   
       
        this.departIds = value
           this.getPValue()
       },
       changePValue(value){
         console.log('PValue',value)
         this.productBelong = value
          this.getPCode()
       },
       changeSort(value){
         
       },
      getname(){
       var params = {};
       params.type = 2;
        getAction(this.url.dtPCode, params).then((res) => {
        var objlist = [];
        if (res.success) {
          for(var i=0;i<res.result.length;i++){
            var obj = {};
            obj.value = res.result[i].id;
            obj.text = res.result[i].text;
            obj.title = res.result[i].text;
            objlist.push(obj);
          }
          console.log("objlist---->>>",objlist)
            this.textName = objlist;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
        
      })

      },
      getPValue(){
         var params = {};   
        params.codeType = "code600000";
        params.codeSortId = "600000";
        params.orgName = this.departIds
        //params.content1 = store.getters.userInfo.id;
    getAction(this.url.PValue, params).then((res) => {
      // console.log('res',res)
        var objlist = [];
        if (res.success) {
          for(var i=0;i<res.result.length;i++){
            var obj = {};
            obj.value = res.result[i].dtPCode;
            obj.text = res.result[i].dtPValue;
            obj.title = res.result[i].dtPValue;
            objlist.push(obj);
          }
          console.log("objlist---->>>",objlist)
            this.PValue = objlist;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
        
      })
      },
      getPCode(){
          var params = {};   
        params.codeType = "code100000";
        params.codeSortId = "100000";
        //params.content1 = store.getters.userInfo.id;
        params.productBelong = this.productBelong
    getAction(this.url.PValue, params).then((res) => {
      console.log('res',res)
        var objlist = [];
        if (res.success) {
          for(var i=0;i<res.result.length;i++){
            var obj = {};
            obj.value = res.result[i].dtPCode;
            obj.text = res.result[i].dtPValue;
            obj.title = res.result[i].dtPValue;
            objlist.push(obj);
          }
          console.log("objlist---->>>",objlist)
            this.PCode = objlist;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
        
      })

      }
    },
    mounted(){
      this.getname()
      this.getPCode()
    
    }
  }
</script>